package cn.fl.bizflow.service.impl;

import cmm.comm.facade.framework.api.ISysCtrlCfgApiService;
import cmm.comm.facade.framework.condition.SysCtrlCfgQueryCondition;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.constant.SysOrgConstant;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.bizflow.service.ISysBizFlowRlService;
import cn.fl.bizflow.transform.SysBizFlowRlVOTransform;
import cn.fl.bizflow.vo.SysBizFlowRlVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.constant.PrjLnkCompConstant;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.flow.facade.flow.api.IFlowQueryApiService;
import fl.flow.facade.flow.condition.SysFlowSHQueryCondition;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.flow.facade.flow.dto.SysFlowSHDTO;
import fl.spl.facade.bizflow.api.ISysBizFlowRlDMLApiService;
import fl.spl.facade.bizflow.api.ISysBizFlowRlQueryApiService;
import fl.spl.facade.bizflow.condition.SysBizFlowRlQC;
import fl.spl.facade.bizflow.constant.SysBizFlowRlConstant;
import fl.spl.facade.bizflow.dto.SysBizFlowRlDTO;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.framework.enums.SplAudStsCdEnum;
import fl.spl.facade.framework.enums.bizflow.SplBizFlowRelEnum;
import fl.spl.facade.spl.constant.SplInfoConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 业务数据与流程权限关系表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class SysBizFlowRlServiceImpl implements ISysBizFlowRlService {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(ISysBizFlowRlService.class);

    @Autowired
    private ISysBizFlowRlQueryApiService sysBizFlowRlQueryApiService;

    @Autowired
    private ISysBizFlowRlDMLApiService sysBizFlowRlDMLApiService;

    @Autowired
    private IFlowQueryApiService flowQueryApiService;

    @Autowired
    private ISysOrgApiService sysOrgApiService;

    @Autowired
    private ISysCtrlCfgApiService sysCtrlCfgApiService ;



    @Override
    public boolean batchAdd(List<SysBizFlowRlVO> list) throws BaseException {
        if (CollectionUtils.isEmpty(list)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "批量新增数据为空.");
        }
        return this.sysBizFlowRlDMLApiService.batchAddBizFlowRl(SysBizFlowRlVOTransform.toDTOList(list));
    }

    @Override
    public List<SysBizFlowRlVO> searchList(SysBizFlowRlQC qc) throws BaseException {
        List<SysBizFlowRlDTO> list = this.sysBizFlowRlQueryApiService.searchList(qc);
        return SysBizFlowRlVOTransform.toVOList(list);
    }

    @Override
    public SysBizFlowRlVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
            return null;
        }
        //TODO 业务逻辑
        SysBizFlowRlDTO dto = null;//this.自定义ApiService.selectById(id);
        return SysBizFlowRlVOTransform.toVO(dto);
    }

    @Override
    public Long add(SysBizFlowRlVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
            return null;
        }
        //return this.sysBizFlowRlApiService.add(SysBizFlowRlVOTransform.toDTO(entity));
        //TODO 业务逻辑
        return null;
    }

    @Override
    public boolean removeById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
            return false;
        }
        //return this.sysBizFlowRlApiService.removeById(id);
        //TODO 业务逻辑
        return false;
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) {
            BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
            return false;
        }
        ;
        return false;
    }

    @Override
    public boolean modify(SysBizFlowRlVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
            return false;
        }
        //return this.sysBizFlowRlApiService.modify(SysBizFlowRlVOTransform.toDTO(entity));
        //TODO 业务逻辑
        return false;
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        //return this.sysBizFlowRlApiService.searchListPage(page);
        //TODO 业务逻辑
        return null;
    }

    @Override
    public DataResultVO addAndApply(List<SysBizFlowRlVO> list, Long splId, Integer num1, Integer num2) throws BaseException {
        DataResultVO resultVO = new DataResultVO();
        FlowStartParamsDTO flowStartParamsDTO = this.getFlowStartParams(FlowConstant.FLOW_BUSINESS_TYP_SPL_BSC_INFO, FlowConstant.FLOW_MODEL_TYP_SPL_AUTHORITH);

        Map<String, Object> startParams = new HashMap<String, Object>();
        Integer isProMgr = 0;

        if (CurrentThreadContext.getCurrentOrgCd().length() == PrjLnkCompConstant.PRO_COMP_MGR_ROLE_ID || (num1 != null && num1.intValue() < 10 && num2 != null && num2.intValue() < 10)) {
            isProMgr = BaseConstant.IS_YESNO_YES;
        }
        startParams.put("isProvMgr", isProMgr);
        flowStartParamsDTO.setStartParams(startParams);
        flowStartParamsDTO.setSysCd(CurrentThreadContext.getCurrentSysCd());
        flowStartParamsDTO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        DataResultDTO resultdto = this.sysBizFlowRlDMLApiService.addAndApply(SysBizFlowRlVOTransform.toDTOList(list), flowStartParamsDTO, splId);
        resultVO.setData(resultdto.getData());
        resultVO.setInfo(resultdto.getInfo());
        return resultVO;
    }

    /******************************************************
     * 获取发起流程对象
     *
     * @param businessType
     *            :业务类型(FlowConstant.FLOW_BUSINESS_TYP_SETTLE)
     * @param modelTyp
     *            :流程业务分类 正常结清:FlowConstant.FLOW_MODEL_TYP_SETTLE_NORMAL
     *            提前结清:FlowConstant.FLOW_MODEL_TYP_SETTLE_ADVANCE
     *            强制结清:FlowConstant.FLOW_MODEL_TYP_SETTLE_FORCE
     * @return
     * @throws BaseException
     *****************************************************/
    private FlowStartParamsDTO getFlowStartParams(String businessType,
                                                  String modelTyp) throws BaseException {
        if (StringUtils.isEmpty(modelTyp)) {
            throw new BaseException(
                    BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "流程业务分类不能为空");
        }
        FlowStartParamsDTO flowStartParamsDTO = new FlowStartParamsDTO();
        // 流程定义KEY和流程定义ID不定义 流程发起的时候会根据模块类型查找唯一流程去发起
        // flowStartParamsDTO.setProcDefKey("");
        // flowStartParamsDTO.setProcDefId("");
        flowStartParamsDTO.setAudStsCd(FlowConstant.FLOW_CHECK_STATE_IN);
        flowStartParamsDTO.setWordsInTheTable(SplInfoConstant.SPL_FLOW_S_H_SPL);
        flowStartParamsDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
        flowStartParamsDTO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        flowStartParamsDTO.setSysCd(CurrentThreadContext.getCurrentSysCd());
        flowStartParamsDTO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
        flowStartParamsDTO.setInitiator(String.valueOf(CurrentThreadContext
                .getCurrentUserId()));
        flowStartParamsDTO.setInitiatorNm(CurrentThreadContext
                .getCurrentUserName());
        flowStartParamsDTO
                .setProcPriority(FlowConstant.FLOW_PRIORITY_EMERGENCY);// 优先级,可以在界面定义
        flowStartParamsDTO.setBusinessType(businessType);// TODO 确认用不用
        flowStartParamsDTO.setModelTyp(modelTyp);// 需要和流程配置中的模块类型匹配
        return flowStartParamsDTO;
    }

    @Override
    public boolean batchAddForFlow(List<SysBizFlowRlVO> list) throws BaseException {
        return this.sysBizFlowRlDMLApiService.batchAddForFlow(SysBizFlowRlVOTransform.toDTOList(list));
    }

    /**
     * 保存并申请流程权限变更流程
     *
     * @param buOrgCdFlowRlMap   事业部与经销商流程关系信息 MAP
     * @param splId              经销商id
     * @param flowNoAndSysDicMap 权限流程编号及数据字典配置MAP
     * @param currBuOrgCd        当前事业部CODE
     * @return
     * @throws BaseException
     * @author create by jiangzg
     * @date 2019-04-09 14:36:54
     * num1->支持不上牌资产数量
     * num2->超期30天未上牌车辆数量
     * version 2.0.0
     */
    @Override
    public DataResultVO addAndApplySplBizFlowRl(Map<String,List<SysBizFlowRlVO>> buOrgCdFlowRlMap, Long splId, Map<String, SysDictionaryDTO> flowNoAndSysDicMap,String currBuOrgCd) throws BaseException {
        DataResultVO resultVO = new DataResultVO();
        //发起经销商放款流程权限审核流程事业部CODE
        List<String> buOrgCdList = new ArrayList<>(buOrgCdFlowRlMap.size()) ;
        if(StringUtils.isNotEmpty(currBuOrgCd) && currBuOrgCd.equals(SplSystemConstant.DEFAULT_ORG_CD)){
            for(Map.Entry<String,List<SysBizFlowRlVO>> entry : buOrgCdFlowRlMap.entrySet()){
                buOrgCdList.add(entry.getKey()) ;
            }
        }else{
            buOrgCdList.add(currBuOrgCd);
        }

        //获取对应事业部是否存在流程中的流程
        //流程分类业务类型
        List<String> busTypCdList = SplBizFlowRelEnum.getBusTypListByBuOrgCd(buOrgCdList) ;
        SysFlowSHQueryCondition flowSHQueryCondition = new SysFlowSHQueryCondition();
        flowSHQueryCondition.setDataId(splId);
        flowSHQueryCondition.setWordsInTheTable(SplInfoConstant.SPL_FLOW_S_H_SPL);
        flowSHQueryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        flowSHQueryCondition.setStatus(BaseConstant.IS_YESNO_YES);
        flowSHQueryCondition.setBusTypList(busTypCdList);
        List<SysFlowSHDTO> flowSHList = this.flowQueryApiService.searchFlowSHList(flowSHQueryCondition);
        //TODO 存在流程中事业部codeList,没有进行流程中权限信息更新
        List<String> existFlowBuOrgCds = null ;
        if (CollectionUtils.isNotEmpty(flowSHList)) {
            existFlowBuOrgCds = new ArrayList<>(flowSHList.size()) ;
            for(SysFlowSHDTO sysFlowSHDTO : flowSHList){
                if(SplAudStsCdEnum.CHECKING.getStatus().equals(sysFlowSHDTO.getAudStsCd()) || SplAudStsCdEnum.REJECT.getStatus().equals(sysFlowSHDTO.getAudStsCd())){
                    existFlowBuOrgCds.add(sysFlowSHDTO.getBuOrgCd());
                    buOrgCdList.remove(sysFlowSHDTO.getBuOrgCd()) ;
                    if(CollectionUtils.isMapNotEmpty(buOrgCdFlowRlMap) && buOrgCdFlowRlMap.containsKey(sysFlowSHDTO.getBuOrgCd())){
                        buOrgCdFlowRlMap.remove(sysFlowSHDTO.getBuOrgCd());
                    }
                }
            }
        }
        //查询所有事业部CODE与名称-Map
        List<SysOrgDTO> dtoList = this.sysOrgApiService.searchListByOrgTypCd(CurrentThreadContext.getFlServiceSysCd());
        Map<String, String> buOrgCdNmMap = null;
        if (CollectionUtils.isNotEmpty(dtoList)) {
            buOrgCdNmMap = new HashMap<>(dtoList.size());
            for (SysOrgDTO dto : dtoList) {
                buOrgCdNmMap.put(dto.getOrgCd(), dto.getOrgDispNm());
            }
        }
        StringBuilder info = null ;
        if(CollectionUtils.isEmpty(buOrgCdList)){
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            info = new StringBuilder(existFlowBuOrgCds.size());
            for(String buOrgCd : existFlowBuOrgCds){
                if(null != buOrgCdNmMap && buOrgCdNmMap.containsKey(buOrgCd)){
                    info.append(buOrgCdNmMap.get(buOrgCd)).append(";");
                }
            }
            resultVO.setInfo("事业部["+ info.toString() + "]因存在审核中流程，不支持权限修改.");
            BaseLogUtils.info(logger,"addAndApplySplBizFlowRl","事业部["+ info.toString() + "]因存在审核中流程，不支持权限修改.",CurrentThreadContext.getCurrentUserName());
            return resultVO ;
        }
        //封装事业部与流程实例关系Map
        Map<String,FlowStartParamsDTO> buOrgCdFlowStartParamMap = new HashMap<>(buOrgCdList.size()) ;
        for(String buOrgCd : buOrgCdList){
            SplBizFlowRelEnum splBizFlowRelEnum = SplBizFlowRelEnum.getEnumByBuOrgCd(buOrgCd) ;
            if(null == splBizFlowRelEnum){
                existFlowBuOrgCds.add(buOrgCd) ;
                if(CollectionUtils.isMapNotEmpty(buOrgCdFlowRlMap) && buOrgCdFlowRlMap.containsKey(buOrgCd)){
                    buOrgCdFlowRlMap.remove(buOrgCd);
                }
            }else{
                //封装流程BEAN
                buOrgCdFlowStartParamMap.put(buOrgCd,this.getFlowStartParams(splBizFlowRelEnum.getBusinessTyp(),splBizFlowRelEnum.getBusTypCd()));
            }
        }

        if(CollectionUtils.isMapEmpty(buOrgCdFlowStartParamMap)){
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            info = new StringBuilder(existFlowBuOrgCds.size());
            for(String buOrgCd : existFlowBuOrgCds){
                if(null != buOrgCdNmMap && buOrgCdNmMap.containsKey(buOrgCd)){
                    info.append(buOrgCdNmMap.get(buOrgCd)).append(";");
                }
            }
            resultVO.setInfo("事业部["+ info.toString() + "]因存在审核中流程或无发起流程业务类型不支持权限修改.");
            BaseLogUtils.info(logger,"addAndApplySplBizFlowRl","事业部["+ info.toString() + "]因存在审核中流程或无发起流程业务类型不支持权限修改.",CurrentThreadContext.getCurrentUserName());
            return resultVO ;
        }
        //流程权限变更，支持未上牌资产数量，及超期30天未上牌车辆数量
        Map<String, Object> startParams = null ;
        //
        Map<String,List<SysBizFlowRlDTO>> buOrgCdFlowRlDtoMap = null ;
        if (CollectionUtils.isMapNotEmpty(flowNoAndSysDicMap) && CollectionUtils.isMapNotEmpty(buOrgCdFlowRlMap)) {
            buOrgCdFlowRlDtoMap = new HashMap<>(buOrgCdFlowRlMap.size()) ;
            for(Map.Entry<String,List<SysBizFlowRlVO>> entry : buOrgCdFlowRlMap.entrySet()){
                buOrgCdFlowRlDtoMap.put(entry.getKey(), SysBizFlowRlVOTransform.toDTOList(entry.getValue())) ;
                //流程分支标识 0：区域总审核，之后风险总审核 1：风险总审核
                Integer isProMgr = BaseConstant.IS_YESNO_YES;
                List<SysBizFlowRlVO> bizFlowRlVOS = entry.getValue() ;
                String buOrgCd = entry.getKey() ;
                for(SysBizFlowRlVO flowRlVO : bizFlowRlVOS){
                    if (StringUtils.isNotEmpty(flowRlVO.getFlowNum()) || StringUtils.isNotEmpty(flowRlVO.getExpireUncardNum())) {
                        if (flowNoAndSysDicMap.containsKey(flowRlVO.getPDefKey())) {
                            if (StringUtils.isNotEmpty(flowRlVO.getFlowNum())) {
                                if (StringUtils.isEmpty(flowNoAndSysDicMap.get(flowRlVO.getPDefKey()).getCodeAttr1()) || flowRlVO.getFlowNum().compareTo(Integer.valueOf(flowNoAndSysDicMap.get(flowRlVO.getPDefKey()).getCodeAttr1())) >= 0) {
                                    isProMgr = BaseConstant.IS_YESNO_NO;
                                    break;
                                } else if (flowRlVO.getFlowNum().intValue() == -1) {
                                    isProMgr = BaseConstant.IS_YESNO_NO;
                                    break;
                                }
                            }
                            if (StringUtils.isNotEmpty(flowRlVO.getExpireUncardNum())) {
                                if (StringUtils.isEmpty(flowNoAndSysDicMap.get(flowRlVO.getPDefKey()).getCodeAttr2()) || flowRlVO.getExpireUncardNum().compareTo(Integer.valueOf(flowNoAndSysDicMap.get(flowRlVO.getPDefKey()).getCodeAttr2())) >= 0) {
                                    isProMgr = BaseConstant.IS_YESNO_NO;
                                    break;
                                } else if (flowRlVO.getExpireUncardNum().intValue() == -1) {
                                    isProMgr = BaseConstant.IS_YESNO_NO;
                                    break;
                                }
                            }
                        }
                    }
                }
                startParams = new HashMap<String, Object>(4);
                startParams.put("isProvMgr", isProMgr);
                if(CollectionUtils.isMapNotEmpty(buOrgCdFlowStartParamMap) && buOrgCdFlowStartParamMap.containsKey(buOrgCd)){
                    buOrgCdFlowStartParamMap.get(buOrgCd).setStartParams(startParams);
                }
            }
        }else{
            startParams = new HashMap<String, Object>(4);
            startParams.put("isProvMgr", BaseConstant.IS_YESNO_YES);
            if(CollectionUtils.isMapNotEmpty(buOrgCdFlowStartParamMap)){
                for(Map.Entry<String,FlowStartParamsDTO> entry : buOrgCdFlowStartParamMap.entrySet()){
                    entry.getValue().setStartParams(startParams);
                }
            }
            buOrgCdFlowRlDtoMap = new HashMap<>(buOrgCdFlowRlMap.size()) ;
            for(Map.Entry<String,List<SysBizFlowRlVO>> entry : buOrgCdFlowRlMap.entrySet()) {
                buOrgCdFlowRlDtoMap.put(entry.getKey(), SysBizFlowRlVOTransform.toDTOList(entry.getValue()));
            }
        }
        BaseLogUtils.info(logger,"addAndApplySplBizFlowRl","保存并申请流程权限变更流程,重新匹配事业部前，发起流程参数buOrgCdFlowStartParamMap = "+JSONObject.toJSONString(buOrgCdFlowStartParamMap),CurrentThreadContext.getCurrentUserName());
        for(Map.Entry<String,FlowStartParamsDTO> entry : buOrgCdFlowStartParamMap.entrySet()){
            entry.getValue().setSysCd(CurrentThreadContext.getCurrentSysCd());
            if(SplSystemConstant.DEFAULT_ORG_CD.equals(currBuOrgCd)){
                entry.getValue().setBuOrgCd(SplBizFlowRelEnum.getBuOrgCdByBusTypCd(entry.getValue().getModelTyp()));
                entry.getValue().setOrgCd(SplBizFlowRelEnum.getBuOrgCdByBusTypCd(entry.getValue().getModelTyp()));
            }else{
                entry.getValue().setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
                entry.getValue().setOrgCd(CurrentThreadContext.getCurrentOrgCd());
            }
        }
        BaseLogUtils.info(logger,"addAndApplySplBizFlowRl","保存并申请流程权限变更流程,重新匹配事业部后，发起流程参数buOrgCdFlowStartParamMap = "+JSONObject.toJSONString(buOrgCdFlowStartParamMap),CurrentThreadContext.getCurrentUserName());
        DataResultDTO resultdto = null ;
        try{
            //TODO 重卡新增判断逻辑，如果不在流程中，1000093沈旭东、1001527张建梅角色可不发起流程直接修改当前经销商放款流程权限
            boolean isNeedStartFlow = true ;
            if(currBuOrgCd.equals(SysOrgConstant.SYS_ORG_CD_ZK)){
                SysCtrlCfgQueryCondition cfgQueryCondition = new SysCtrlCfgQueryCondition();
                cfgQueryCondition.setSysCd(SplSystemConstant.SPL_SYS_CD);
                cfgQueryCondition.setOrgCd(SysOrgConstant.SYS_ORG_CD_ZK);
                cfgQueryCondition.setCtrlCode(SysBizFlowRlConstant.CODE_CTRL_SPL_0016_00000001_ROLES__CD);
                String roleStr = sysCtrlCfgApiService.searchSysCtrlCfgByCtrlCode(cfgQueryCondition);
                if(StringUtils.isNotEmpty(roleStr)){
                    List<String> roleIdList = Arrays.asList(roleStr.split(";"));
                    BaseLogUtils.info(logger, "addAndApplySplBizFlowRl", "新增经销商放款流程权限发起流程，重卡角色配置，roleIdList:"+ JSON.toJSONString(roleIdList), CurrentThreadContext.getCurrentUserName());
                    if(roleIdList.contains(String.valueOf(CurrentThreadContext.getCurrentRoleId()))){
                        isNeedStartFlow = false ;
                    }
                }
            }
            BaseLogUtils.info(logger, "addAndApplySplBizFlowRl", "角色ID："+CurrentThreadContext.getCurrentRoleId()+"新增经销商放款流程权限发起流程，是否需要发起流程标识，isNeedStartFlow:"+ isNeedStartFlow, CurrentThreadContext.getCurrentUserName());
            if(!isNeedStartFlow){
                //重卡直接新增
                resultdto = this.sysBizFlowRlDMLApiService.batchAddBizFlowRlByBuOrgCd(buOrgCdFlowRlDtoMap,buOrgCdList,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentUserName());
                if(null != resultdto && resultdto.getSuccess() == BaseConstant.IS_YESNO_YES){
                    BaseLogUtils.info(logger,"addAndApplySplBizFlowRl","放款流程权限变更修改成功,返回数据，resultdto："+JSON.toJSONString(resultdto),CurrentThreadContext.getCurrentUserName());
                    resultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                    resultVO.setInfo("放款流程权限变更修改成功");
                    return resultVO ;
                }else{
                    BaseLogUtils.info(logger,"addAndApplySplBizFlowRl","放款流程权限变更修改失败,返回数据，resultdto："+(null != resultdto ? JSON.toJSONString(resultdto):"无数据"),CurrentThreadContext.getCurrentUserName());
                    resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                    resultVO.setInfo("放款流程权限变更修改失败！");
                    return resultVO ;
                }
            }else{
                resultdto = this.sysBizFlowRlDMLApiService.addAndApplyByBuOrgCd(buOrgCdFlowRlDtoMap,buOrgCdFlowStartParamMap,splId,buOrgCdList,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentUserName());
            }
        }catch (RpcException rpc){
            BaseLogUtils.info(logger,"addAndApplySplBizFlowRl","放款流程权限变更流程发起异常，RpcException.["+JSONObject.toJSONString(rpc.getMessage())+"]",CurrentThreadContext.getCurrentUserName(),rpc);
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo(rpc.getMessage());
            return resultVO ;
        }catch (BaseException be){
            BaseLogUtils.info(logger,"addAndApplySplBizFlowRl","放款流程权限变更流程发起异常，BaseException.["+JSONObject.toJSONString(be.getMessage())+"]",CurrentThreadContext.getCurrentUserName(),be);
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo(be.getMessage());
            return resultVO ;
        }catch (Exception e){
            BaseLogUtils.info(logger,"addAndApplySplBizFlowRl","放款流程权限变更流程发起异常，Exception.["+JSONObject.toJSONString(e.getMessage())+"]",CurrentThreadContext.getCurrentUserName(),e);
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo(e.getMessage());
            return resultVO ;
        }
        info = new StringBuilder("事业部[");
        int i = 1 ;
        for(String buOrgCd : buOrgCdList){
            if(null != buOrgCdNmMap && buOrgCdNmMap.containsKey(buOrgCd)){
                info.append(buOrgCdNmMap.get(buOrgCd)).append(";");
            }
            if(i == buOrgCdList.size()){
                info.replace(info.lastIndexOf(";"),info.length(),"");
            }
            i++ ;
        }
        info.append("]放款流程权限变更流程发起成功.");
        if(CollectionUtils.isNotEmpty(existFlowBuOrgCds)){
            info.append("事业部[");
            i = 1 ;
            for(String buOrgCd : existFlowBuOrgCds){
                if(null != buOrgCdNmMap && buOrgCdNmMap.containsKey(buOrgCd)){
                    info.append(buOrgCdNmMap.get(buOrgCd)).append(";");
                }
                if(i == existFlowBuOrgCds.size()){
                    info.replace(info.lastIndexOf(";"),info.length(),"");
                }
                i++ ;
            }
            info.append("]因存在审核中流程或无发起流程业务类型不支持权限修改.");
            resultVO.setInfo("事业部["+ info.toString() + "]因存在审核中流程或无发起流程业务类型不支持权限修改.");
        }
        resultVO.setInfo(info.toString());
        BaseLogUtils.info(logger,"addAndApplySplBizFlowRl","放款流程权限变更流程发起成功，返回参数resultVO.["+JSONObject.toJSONString(resultVO)+"]",CurrentThreadContext.getCurrentUserName());
        return resultVO;
    }

    /**
     * 流程中批量添加流程权限记录
     *
     * @param buOrgCdSysBizFlowRlVOMap 经销商流程权限关系list
     * @param opBuOrgCds       操作事业部CODE
     * @param opSysCd         操作系统编码
     * @param userNm          操作人名称
     * @return
     * @throws BaseException
     */
    @Override
    public boolean batchAddBizFlowRlVOForFlow(Map<String,List<SysBizFlowRlVO>> buOrgCdSysBizFlowRlVOMap, List<String> opBuOrgCds, String opSysCd, String userNm) throws BaseException {
        Map<String,List<SysBizFlowRlDTO>> buOrgCdBizFLowRlDTOsMap = null ;
        if(CollectionUtils.isMapNotEmpty(buOrgCdSysBizFlowRlVOMap)){
            buOrgCdBizFLowRlDTOsMap = new HashMap<>(buOrgCdSysBizFlowRlVOMap.size()) ;
            for(Map.Entry<String,List<SysBizFlowRlVO>> entry : buOrgCdSysBizFlowRlVOMap.entrySet()){
                buOrgCdBizFLowRlDTOsMap.put(entry.getKey(),SysBizFlowRlVOTransform.toDTOList(entry.getValue())) ;
            }
        }else{
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_500,"流程中批量添加流程权限记录,业务数据流程权限关系数据为空！");
        }
        DataResultDTO dataResultDTO = this.sysBizFlowRlDMLApiService.batchAddBizFlowRlForFlow(buOrgCdBizFLowRlDTOsMap,opBuOrgCds,opSysCd,userNm);
        if(dataResultDTO != null && dataResultDTO.getSuccess() == BaseConstant.IS_YESNO_YES){
            return true ;
        }else{
            return false ;
        }
    }

    /**
     * 通过事业部系统CODE批量新增业务数据与流程权限关系
     *
     * @param buOrgCdSysBizFLowRlsMap 事业部CODE与经销商流程权限关系list MAP
     * @param opBuOrgCdList           操作事业部CODE List
     * @param opSysCd                 操作系统编码
     * @param opUserNm                操作人名称
     * @return
     * @throws BaseException
     * @author jiangzg
     * @date 2019-06-18 10:16:07
     */
    @Override
    public boolean batchAddBizFlowRlByBuOrgCd(Map<String, List<SysBizFlowRlVO>> buOrgCdSysBizFLowRlsMap, List<String> opBuOrgCdList, String opSysCd, String opUserNm) throws BaseException {
        Map<String,List<SysBizFlowRlDTO>> buOrgCdBizFLowRlDTOsMap = null ;
        if(CollectionUtils.isMapNotEmpty(buOrgCdSysBizFLowRlsMap)){
            buOrgCdBizFLowRlDTOsMap = new HashMap<>(buOrgCdSysBizFLowRlsMap.size()) ;
            for(Map.Entry<String,List<SysBizFlowRlVO>> entry : buOrgCdSysBizFLowRlsMap.entrySet()){
                buOrgCdBizFLowRlDTOsMap.put(entry.getKey(),SysBizFlowRlVOTransform.toDTOList(entry.getValue())) ;
            }
        }else{
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_500,"新增业务数据流程权限关系数据为空！");
        }
        DataResultDTO dataResultDTO = this.sysBizFlowRlDMLApiService.batchAddBizFlowRlByBuOrgCd(buOrgCdBizFLowRlDTOsMap,opBuOrgCdList,opSysCd,opUserNm);
        if(dataResultDTO != null && dataResultDTO.getSuccess() == BaseConstant.IS_YESNO_YES){
            return true ;
        }else{
            return false ;
        }
    }

}
